<?php
declare (strict_types=1);

namespace app\admin\model\store;

use app\common\model\TimeModel;
use think\facade\Db;

/**
 * @mixin \think\Model
 */
class GoodsSku extends TimeModel
{
    //sku信息
    public static function getSkuInfo($goods_sku_id)
    {
        $res = self::where('id', $goods_sku_id)->with('goods')->find();
        $res && $res = $res->toArray();
        return $res;
    }

    //sku减库存
    public static function reduceSkuNum($params)
    {
            foreach ($params['skuInfo'] as $key =>$val) {
                Db::startTrans();
                if ($params['order_type_code'] == 1) {
                    try {
                        $skuStock = self::where('id', $key)->value('sku_stock');
                        $res_sku = self::where('id', $key)->update(['sku_stock' => intval($skuStock) - intval($val)]);
                        $goodId = self::where('id', $key)->value('goods_id');
                        $goodsStock = Goods::where('id', $goodId)->value('goods_stock');
                        $res_good = Goods::where('id', $goodId)->update(['goods_stock' => intval($goodsStock) - intval($val)]);
                        if ($res_sku && $res_good) {
                            $res = true;
                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                    }
                }elseif ($params['order_type_code'] == 2){
                    $pinkSkuStock=PinkConfig::where('id',$params['pink_id'])->value('pink_sku_stock');
                    $updateStpck = $pinkSkuStock - $params['num'];
                    //减掉拼团活动库存
                    try {
                        $res=PinkConfig::where('id',$params['pink_id'])->update(['pink_sku_stock'=>$updateStpck]);
                        if ($res){
                            $res = true;
                        }
                        Db::commit();
                    }catch(\Exception $e){
                        Db::rollback();
                    }
                } elseif($params['order_type_code'] == 3){//减掉秒杀库存
                    try {
                        $stock = Seckill::where([
                            'config_id'=>$params['seckill_id'],
                            'goods_id'=>$key,
                            'status'=>1
                        ])->value('limit');
                        $resSeckill = Seckill::where([
                            'config_id'=>$params['seckill_id'],
                            'goods_id'=>$key,
                            'status'=>1
                        ])->update([
                            'limit_sock' => intval($stock)-intval($val)
                        ]);
                        $skuStock = self::where('id', $key)->value('sku_stock');
                        $resSku = self::where('id', $key)->update([
                            'sku_stock' => intval($skuStock)-intval($val)
                        ]);
                        $goodId = self::where('id',$key)->value('goods_id');
                        $goodsStock = Goods::where('id', $goodId)->value('goods_stock');
                        $resGood = Goods::where('id', $goodId)->update([
                            'goods_stock' => intval($goodsStock)-intval($val)
                        ]);
                        if ($resSku && $resGood && $resSeckill){
                            $res =true;
                        }
                        Db::commit();
                    }catch (\Exception $e){
                        Db::rollback();
                    }
                }elseif ($params['order_type_code'] == 4){
                   $res = true;
                } elseif ($params['order_type_code'] == 5){
                    $pinkSkuStock=PinkConfig::where('id',$params['pink_id'])->value('pink_sku_stock');
                    $updateStpck = $pinkSkuStock - $params['num'];
                    //减掉拼团活动库存
                    try {
                        $res=PinkConfig::where('id',$params['pink_id'])->update(['pink_sku_stock'=>$updateStpck]);
                        if ($res){
                            $res = true;
                        }
                        Db::commit();
                    }catch(\Exception $e){
                        Db::rollback();
                    }
                }
            }
        return $res;
    }

    //sku回库存
    public static function returnSkuNum($params)
    {
        $skuStock = self::where('id', $params['goods_sku_id'])->value('sku_stock');
        $res = self::where('id', $params['goods_sku_id'])->update(['sku_stock' => $skuStock + $params['total_num']]);
        return $res;
    }

    //查询库存

    public function good()
    {
        return $this->belongsTo(Goods::class, 'goods_id', 'id');
    }


    public function goods()
    {
        return $this->belongsTo(Goods::class, 'goods_id', 'id')->bind(['goods_name', 'goods_desc', 'image']);
    }

}
